package com.example.demo.controller

import com.example.demo.dto.LoginDto
import com.example.demo.dto.LoginResponseDto
import com.example.demo.dto.RegisterDto
import com.example.demo.entity._User
import com.example.demo.repo.ArtworkRepo
import com.example.demo.repo.TitleRepo
import com.example.demo.repo.UserRepo
import org.springframework.beans.factory.annotation.Autowired
import org.springframework.web.bind.annotation.GetMapping
import org.springframework.web.bind.annotation.PostMapping
import org.springframework.web.bind.annotation.RequestBody
import org.springframework.web.bind.annotation.RestController
import java.util.UUID

@RestController
class UserController {
    val dict = mutableMapOf<String, Long>()

    @Autowired
    lateinit var userRepo: UserRepo//想用repo就要用注入autowire的方式

    @PostMapping("/register")
    //repo就是负责交互的东西，有repo才能存储
    fun addUser(@RequestBody dto: RegisterDto) {
        println(dto)

        userRepo.save(
            _User(
                name = dto.username,
                Password = dto.password,
                account = dto.account,
            )
        )
    }

    @PostMapping("/login")
    //repo就是负责交互的东西，有repo才能存储
    fun login(@RequestBody dto: LoginDto): LoginResponseDto {
        val randomString = UUID.randomUUID().toString()
        val user = userRepo.findByAccount(dto.account).first()
        if (user.Password == dto.password) {
            dict[randomString] = user.uid!!
            return LoginResponseDto(
                randomString, tag = user.tag, username = user.name
            )
        }
        throw Exception("error")
    }
}